<?php
class columnApp extends core
{
	public function indexAction($arg)
	{
		if (!isset($arg['classid'])) errorHandle::ParameterErrors('params');

        $show       = $arg['show']	= isset($arg['show']) ? abs($arg['show']) : 20;
        $page       = isset($arg['page']) ? abs($arg['page']) : 1;
        $order		= isset($arg['order']) ? ($arg['order']) : 'id desc';
        //
        $columns	= base::init('column')->fetchAll();
        $columns	= base::init('column')->listcls($columns,0);
		$column		= $columns[$arg['classid']];

		$website	= base::init('website')->fetchRow(array('wid'=>$column['wid']));
		$mode   	= base::init('mode')->fetchAll();
        $mode       = sort_combine($mode, 'mid', true);
        $attr		= base::init('website')->getConfig($website['domain']);
        
        $tbname		= $mode[$column['modid']]['tbname'];
        $list		= $tbname.'s';

        //list($$list,$num) = base::init($tbname)->columnList($arg);
        $list		= base::init($tbname)->fetchAll("classid={$arg['classid']}",$order,$show,abs(($page-1)*$show));
        $list		= base::init($tbname)->getTempList($list,$columns,$attr);
        $num        = base::init($tbname)->getCount(array('classid'=>$arg['classid']));
		$template	= $this->template();

        //isset($arg['field']) && $template->assign('field',$arg['field']);
        //isset($arg['value']) && $template->assign('value',$arg['value']);
        //isset($arg['order']) && $template->assign('order',$arg['order']);

		$template->assign('column', $column);
		$template->assign('columns', $columns);
		$template->assign('tbname', $tbname);
		$template->assign('mode', $mode);
		$template->assign('website', $website);
		$template->assign('attr', $attr);
		$template->assign('list', $list);
		$template->assign('num', $num);
		$template->assign('page', $page);
		$template->assign('show', $show);
		$template->assign('pagecount', ceil($num/$show));
		$template->display('column_index.php');
	}
	
	public function addAction($arg)
	{
		if (isPost())
		{
	        $classid	= base::init('column')->insert($arg);
	        $this->refresh('添加成功', "index.php?m=column&classid=$classid");
		}
		$columns	= base::init('column')->fetchAll(array('wid'=>$arg['wid']));
        //$columns	= base::init('column')->listcls($columns,0);
		$website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
		$mode   	= base::init('mode')->fetchAll();
        $mode       = sort_combine($mode, 'mid', true);
        $attr		= base::init('website')->getConfig($website['domain']);
        
        $template	= $this->template();
		$template->assign('columns', $columns);
		$template->assign('mode', $mode);
		$template->assign('website', $website);
		$template->assign('attr', $attr);
		$template->display('column_add.php');
	}


    public function getAction($arg)
    {
        $columns    = base::init('column')->fetchAll(array('wid'=>$arg['wid']));
        $columns    = base::init('column')->listcls($columns,0);
        $website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
        
        $template	= $this->template();
        $template->assign('columns', $columns);
        $template->assign('website', $website);
		$template->display('column_get.php');
    }

    public function editAction($arg)
    {
    	
        $where  = "classid={$arg['classid']}";
        
        $column	= base::init('column')->fetchRow($where);
        
        if (!$arg['display'])
        {
	        if ($column['sonclassid'])
	        {
	        	base::init('column')->update( array('display'=>$arg['display']),array('classid'=>$column['sonclassid']));
	        }
        }
        
        base::init('column')->update($arg, $where);
        $this->refresh('修改成功', "index.php?m=column&classid={$arg['classid']}");
    }
    
    public function deleteAction($arg)
    {
    	base::init('column')->delete(array('classid'=>$arg['classid']));
    	exit('1');
    }
}
